<template>
  <div class="config-margin">
    <affix :label="$t('configLazyLoad.robotSeven.waitLearnQuestion')">
      <div class="search" @keyup.enter="search">
        <el-input
          size="mini"
          :placeholder="$t('configLazyLoad.robotSeven.pleaseInputSearchContent')"
          v-model="searchObj.name"
          style="width:300px"
        >
          <i
            class="el-input__icon el-icon-search"
            slot="suffix"
            @click="search">
          </i>
        </el-input>
      </div>
      <common-table :tableHead="tableHead" :data="tableData" :pagination="pagination" @operate="operate" @turnPage="search">
      </common-table>
    </affix>
    <el-dialog :close-on-click-modal="false" :class="commonDialog.type ==='view' ? 'unauto' : ''" :title="commonDialog.title" :visible.sync="commonDialog.isShow">
      <webchat-box v-if="commonDialog.type ==='view'" :webchatContent="current"></webchat-box>
      <relevant-questions
        v-else-if="commonDialog.type ==='relevance'"
        @close="close"
        @saveReleventQuestion="saveReleventQuestion"
        :refresh="commonDialog.random"
        :hiddenEditName="true"
      ></relevant-questions>
      <add-question v-if="commonDialog.type === 'add'" @dialogaddquestion="dialogaddquestion" @formData="formData" :oldData="oldData"></add-question>
    </el-dialog>
  </div>
</template>
<script>
  import tree from '../agentConfig/tree/Base/Tree.vue'
  import affix from '../../../ui-modules/affix/Affix.vue'
  import commonTable from './base/CommonTable'
  import webchatBox from './base/WebchatBox.vue'
  import AddQuestion from './AddQuestion'
  import relevantQuestions from './likeness.vue'
  import * as uuid from 'uuid'
  export default {
    name: 'intelligentStudy',
    data () {
      return {
        pagination: {
          currentPage: 0,
          count: 0,
          totalPage: 0
        },
        searchObj: {
          name: '',
          page: 1,
          limit: 10
        },
        commonDialog: {
          isShow: false,
          title: '',
          type: '',
          size: '',
          random: 0
        },
        currentOperate: {},
        current: {},
        treeData: {},
        tableData: [],
        tableHead: [
          {prop: 'name', label: this.$t('configLazyLoad.robotSeven.unKnownQuestion'), special: 'html'},
          {prop: 'platform', label: this.$t('report.imReportFrom')},
          {prop: 'createTime', label: this.$t('configLazyLoad.robotSeven.creatTime')},
          {prop: 'count', label: this.$t('configLazyLoad.robotSeven.askNumber')},
          {prop: 'operate', label: this.$t('sms.operate'), special: 'operate', operate: [{name: this.$t('configLazyLoad.robotSeven.context'), type: 'view'}, {name: this.$t('configLazyLoad.robotSeven.ignore'), type: 'ignore'}, {name: this.$t('configLazyLoad.robotSeven.related'), type: 'relevance'}, {name: this.$t('configLazyLoad.robotSeven.add'), type: 'add'}]}
        ],
        oldData: {},
        unKnownQuesId: ''
      }
    },
    components: {
      tree,
      commonTable,
      affix,
      webchatBox,
      relevantQuestions,
      AddQuestion
    },
    methods: {
      close () {
        this.commonDialog.isShow = false
      },
      operate (event) {
        let type = event.type
        this.unKnownQuesId = event.val._id
        this.commonDialog.isShow = true
        this.commonDialog.random = Math.random()
        this.currentOperate = event.val
        if (type === 'view') {
          this.commonDialog.type = 'view'
          this.commonDialog.size = 'small'
          this.commonDialog.title = this.$t('business.sessionContent')
          this.$store.dispatch('queryQuestionAnswerDetail', {cid: event.val.cid}).then((res) => {
            if (res.success) {
              this.current = res
            }
          })
        } else if (type === 'ignore') {
          this.ignoreQuestion(event.val._id)
          this.commonDialog.isShow = false
        } else if (type === 'relevance') {
          this.commonDialog.type = 'relevance'
          this.commonDialog.size = 'small'
          this.commonDialog.title = this.$t('configLazyLoad.robotSeven.relatedQues')
        } else if (type === 'add') {
          let valName = event.val.name || ''
          if (valName) {
            valName = valName.replace(/<label7moor>/ig, '').replace(/<\/label7moor>/g, '')
          }
          this.oldData = {
            _id: event.val._id,
            name: valName,
            answer: '',
            likeness: [],
            status: '1',
            type: '',
            isPush: '0',
            referQuestions: [],
            isAdd: true
          }
          this.commonDialog.type = 'add'
          this.commonDialog.size = 'small'
          this.commonDialog.title = this.$t('configLazyLoad.robotSeven.add')
        }
      },
      ignoreQuestion (_id) {
        this.$confirm(this.$t('configLazyLoad.robotSeven.warningTip1'), this.$t('configLazyLoad.robotSeven.ignore'), {
          confirmButtonText: this.$t('public.confirm'),
          cancelButtonClass: 'primary_btn',
          confirmButtonClass: 'primary_btn',
          cancelButtonText: this.$t('public.cancel'),
          type: 'warning'
        })
          .then(() => {
            this.$store.dispatch('unknownStudyIgnore', {qId: _id})
              .then(resp => {
                if (resp.success) {
                  this.search()
                  this.$message({
                    type: 'success',
                    message: this.$t(this.$t('configLazyLoad.robotSeven.ignoreSuccess'))
                  })
                }
              })
          }).catch(() => {})
      },
      saveReleventQuestion (form) {
        let data = {questionId: this.currentOperate._id, likeness: [this.currentOperate.name], type: form.type, qId: form.qId}
        this.$store.dispatch('unknownRelevanceQuestion', data).then((res) => {
          if (res.success) {
            this.commonDialog.isShow = false
            this.$message.success(this.$t('configLazyLoad.robotSeven.relationToQuestionSuccess'))
            this.search(1)
          }
        })
      },
      search (page) {
        this.searchObj.page = typeof page === 'number' ? page : 1
        this.$store.dispatch('unknownStudyQueryQuestions', this.searchObj).then((res) => {
          if (res.success) {
            this.pagination.count = res.count
            this.pagination.currentPage = this.searchObj.page
            let list = res.list
            let arr = []
            for (let i = 0; i < list.length; i++) {
              let question = list[i]
              arr.push({cid: question.cid, operate: '1', _id: question._id, name: question.name, count: question.count, platform: question.platform, createTime: question.createTime})
            }
            this.tableData = arr
          }
        })
      },
      dialogaddquestion (showAdd) {
        this.commonDialog.isShow = showAdd
      },
      formData (form) {
        if (this.commonDialog.type === 'add') {
          form._id = uuid.v1()
          let unKnownQuesId = this.unKnownQuesId
          form.unKnownQuesId = unKnownQuesId
          this.$store.dispatch('addUnknowQuestion', form).then((rep) => {
            if (rep.success) {
              this.dialogaddquestion(false)
              this.$message(this.$t('configLazyLoad.robotSeven.addQuesSuccess'))
              this.search()
            } else {
              this.$message({
                message: rep.message,
                type: 'error'
              })
            }
          })
        }
      }
    },
    deactivated () {
      this.commonDialog.isShow = false
    },
    beforeMount () {
      this.search()
    }
  }
</script>
<style lang="stylus" scoped>
  .config-margin
    margin 20px 20px 0
  .search
    padding-bottom 10px
</style>
